package com.gray.blog.service.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gray.blog.common.enums.DelFlagTypeEnum;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.enums.LoginFlagEnum;
import com.gray.blog.common.enums.sys.RoleTypeEnum;
import com.gray.blog.common.result.ModelResult;
import com.gray.blog.common.util.BeanUtil;
import com.gray.blog.common.util.PagerUtil;
import com.gray.blog.common.util.ResultUtil;
import com.gray.blog.common.util.StringUtil;
import com.gray.blog.dao.sys.SysRoleDao;
import com.gray.blog.dao.sys.SysUserDao;
import com.gray.blog.dao.sys.SysUserRoleDao;
import com.gray.blog.entity.sys.SysUserEntity;
import com.gray.blog.entity.sys.SysUserRoleEntity;
import com.gray.blog.result.sys.GetPageUserResult;
import com.gray.blog.result.sys.GetSysUserByIdResult;
import com.gray.blog.result.sys.GetUserInfoByNameResult;
import com.gray.blog.result.sys.SysUserDetailResult;
import com.gray.blog.result.sys.UpdateUserInfoResult;
import com.gray.blog.service.BaseService;
import com.gray.blog.vo.sys.AddSysUserVO;
import com.gray.blog.vo.sys.GetPageUserVO;
import com.gray.blog.vo.sys.GetUserInfoByNameVO;
import com.gray.blog.vo.sys.UpdateSysUserInfoVO;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SysUserService extends BaseService<SysUserDao, SysUserEntity> {

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    public ModelResult<List<SysUserEntity>> getAllSysUser() {
        return new ModelResult<>(ErrorCodeEnum.SUCCESS, super.getAllData());
    }

    public ModelResult<GetSysUserByIdResult> getSysUserById(String id) {
        SysUserEntity sysUserEntity = super.getById(id);
        if (sysUserEntity == null) {
            log.warn("SysUserService.getSysUserById sysUserEntity is null id:{}", id);
            return new ModelResult<>(ErrorCodeEnum.SYS_USER_NOT_FOUND);
        }
        GetSysUserByIdResult getSysUserByIdResult = BeanUtil.copy(sysUserEntity, GetSysUserByIdResult.class);
        ResultUtil.buildResultTime(getSysUserByIdResult);
        return new ModelResult<>(ErrorCodeEnum.SUCCESS, getSysUserByIdResult);
    }

    public ModelResult addUser(AddSysUserVO vo) {
        SysUserEntity sysUserEntity = BeanUtil.copy(vo, SysUserEntity.class);
        sysUserEntity.setId(StringUtil.getUUID());
        sysUserEntity.setCreateBy(vo.getUserId());
        sysUserEntity.setUpdateBy(vo.getUserId());
        sysUserEntity.setLoginFlag(LoginFlagEnum.ALLOW_LOGIN.getType());
        sysUserEntity.setDelFlag(DelFlagTypeEnum.NOT_DELETE.getType());
        // 分配权限
        SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
        sysUserRoleEntity.setId(StringUtil.getUUID());
        sysUserRoleEntity.setRoleId(sysRoleDao.getRoleByRoleType(RoleTypeEnum.ORDINARY_USER.getRoleType()).getId());
        sysUserRoleEntity.setCreateBy(vo.getUserId());
        sysUserRoleEntity.setUpdateBy(vo.getUserId());
        sysUserRoleEntity.setDelFlag(DelFlagTypeEnum.NOT_DELETE.getType());
        sysUserRoleDao.insert(sysUserRoleEntity);
        sysUserDao.insert(sysUserEntity);
        return ModelResult.newSuccess();
    }

    public ModelResult<GetPageUserResult> getPageUser(GetPageUserVO vo) {
        IPage<SysUserEntity> pager = super.findItemByPage(vo.getPageNum(), vo.getPageSize(), vo.getKeyWordType(), vo.getKeyWord());
        PagerUtil<GetPageUserResult> pagerUtil = new PagerUtil<>();
        GetPageUserResult result = pagerUtil.setPageResultByPageObject(pager, new GetPageUserResult(), SysUserDetailResult.class);
        pagerUtil.setPageResultTime(result.getDataList());
        return ModelResult.newSuccess(result);
    }

    public ModelResult deleteUser(String id) {
        if (this.deleteById(id)) {
            return new ModelResult<>(ErrorCodeEnum.SUCCESS);
        } else {
            log.error("BaseService.deleteUser id:{}", id);
            return new ModelResult<>(ErrorCodeEnum.SYSTEM_ERROR);
        }
    }

    public ModelResult<UpdateUserInfoResult> updateUserInfo(UpdateSysUserInfoVO vo) {
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setId(vo.getId());
        sysUserEntity.setLoginName(vo.getLoginName());
        sysUserEntity.setEmail(vo.getEmail());
        sysUserEntity.setPhone(vo.getPhone());
        sysUserEntity.setHeadPhoto(vo.getHeadPhoto());
        sysUserEntity.setRemarks(vo.getRemarks());
        sysUserEntity.setMotto(vo.getMotto());
        int updateUserInfo = sysUserDao.updateUserInfo(sysUserEntity);
        if (updateUserInfo != 1) {
            log.error("SysUserService.updateUserInfo error vo:{}", vo);
            return new ModelResult<>(ErrorCodeEnum.SYSTEM_ERROR);
        }
        UpdateUserInfoResult updateUserInfoResult = new UpdateUserInfoResult();
        updateUserInfoResult.setUpdateOwnInfo(vo.getUserId().equals(vo.getId()));
        return new ModelResult<>(ErrorCodeEnum.SUCCESS, updateUserInfoResult);
    }

    public ModelResult<GetUserInfoByNameResult> getUserInfoByName(GetUserInfoByNameVO vo) {
        SysUserEntity sysUserEntity = sysUserDao.getInfoByLoginName(vo.getUserName());
        if (sysUserEntity == null) {
            log.error("SysUserService.getUserInfoByName error sysUserEntity is null vo:{}", vo);
            return new ModelResult<>(ErrorCodeEnum.SYS_USER_NOT_FOUND);
        }
        return new ModelResult<>(ErrorCodeEnum.SUCCESS, BeanUtil.copy(sysUserEntity, GetUserInfoByNameResult.class));
    }
}
